package com.lepu.app.fun.my;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import org.json.JSONObject;

import android.os.Bundle;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.ListView;

import com.app.utils.LoginUtil;
import com.app.utils.Setting;
import com.core.lib.application.BaseFragmentActivity;
import com.core.lib.core.ApiClient;
import com.core.lib.core.AsyncRequest;
import com.core.lib.widget.MyDialog;
import com.lepu.app.fun.my.adapter.AdapterMyMassegeBbs;
import com.lepu.app.fun.my.bean.BeanMsgBbs;
import com.lepu.app.widget.CustomTopBarNew.OnTopbarNewLeftLayoutListener;
import com.lepu.app.widget.CustomTopBarNew.OnTopbarNewRightButtonListener;
import com.lepu.app.widget.CustomTopBarNew;
import com.lepu.pasm.R;

public class MyMassegeBbsDetailActivity extends BaseFragmentActivity implements
		OnTopbarNewLeftLayoutListener, OnTopbarNewRightButtonListener, AsyncRequest {
	private static final String REQUEST_MSG = "request_msg";
	
	private ListView mListView = null;
	private int mType = 0;
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.my_massege_detail_activity);

		init();
	}

	@Override
	public void onDestroy() {
		super.onDestroy();
	}

	/**
	 * 添加页面动画，需要重写这个方法，
	 */
	@Override
	public void onBackPressed() {
		finish(true);
	}

	@Override
	public void onTopbarLeftLayoutSelected() {
		finish(true);
	}

	@Override
	public void onTopbarRightButtonSelected() {
		cleanData();
	}
	
	private void showHideNoDataLayout(boolean isShow)
	{
		LinearLayout noDataLayout = (LinearLayout) findViewById(R.id.noDataLayout);
		if(noDataLayout != null)
		{
			if(isShow)
			{
				noDataLayout.setVisibility(View.VISIBLE);
				mListView.setVisibility(View.GONE);
			}else{
				noDataLayout.setVisibility(View.GONE);
				mListView.setVisibility(View.VISIBLE);
			}
		}
	}
	
	/**
	 * 初始化方法
	 */
	private void init() {
		// topbar
		CustomTopBarNew topbar = (CustomTopBarNew) findViewById(R.id.topbar);
		topbar.setTopbarTitle("帖子动态");
		//topbar.setRightText("清空");
		topbar.setonTopbarNewLeftLayoutListener(this);
		//topbar.setOnTopbarNewRightButtonListener(this);
		
		mListView = (ListView) findViewById(R.id.dataListView);
		
		refreshView();
		
		requestAlarm();
	}

	/**
	 * 请求消息
	 */
	private void requestAlarm() {
		String url = Setting.getMassegeBbsListUrl();

		Map<String, Object> map = new HashMap<String, Object>();
		JSONObject params = new JSONObject();
		try {
			params.put("DeviceID", Setting.DEVICEID);
			params.put("ApplicationID", Setting.APPLICATIONID);
		    params.put("UserID", LoginUtil.getUserId());
			params.put("LoginToken", LoginUtil.getLoginToken());
			params.put("MessageID", "-1");
			params.put("MessageType", mType);
		} catch (Exception e) {
			
		}
		map.put("ht", params);
		ApiClient.http_post_main(url, map, null, this, REQUEST_MSG, true);
	}

	@Override
	public void RequestComplete(Object arg0, Object data) {
		//解析数据
		ArrayList<BeanMsgBbs> dataArraylist = BeanMsgBbs.parseJsonData((String) data);
		//插入数据库
		for(int i=0; i<dataArraylist.size(); i++){
			BeanMsgBbs.insertMyMessage(dataArraylist.get(i));
		}
		
		refreshView();
	}

	@Override
	public void RequestError(Object arg0, int arg1, String arg2) {
		refreshView();
	}
	
	private void cleanData(){
		new MyDialog(MyMassegeBbsDetailActivity.this)
		.setMessage("确定清空消息列表")
		.setNegativeButton(R.string.app_cancel,
				new View.OnClickListener() {

					@Override
					public void onClick(View v) {

					}
				})
		.setPositiveButton(R.string.app_ok,
				new View.OnClickListener() {

					@Override
					public void onClick(View v) {
						BeanMsgBbs.deleteAllItem(LoginUtil.getUserId());
						refreshView();
						
					}
				}).create(null).show();

	}
	
	private void refreshView(){
		ArrayList<BeanMsgBbs> list = BeanMsgBbs.getMyMessage(LoginUtil.getUserId());
		if(list.size() == 0){
			showHideNoDataLayout(true);
			return;
		}else{
			showHideNoDataLayout(false);
			AdapterMyMassegeBbs adapter = new AdapterMyMassegeBbs(MyMassegeBbsDetailActivity.this, BeanMsgBbs.getMyMessage(LoginUtil.getUserId()));
			mListView.setAdapter(adapter);
		}
			
	}

}
